﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EyesOfTheDragon.Events;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using XRpgLibrary;
using Anthem.GameScreens;

namespace EyesOfTheDragon.Components
{
    public class PauseManager :IUpdate, IDraw
    {
        public bool Enabled
        {
            get
            {
                return _enabled;
            }
            set
            {
                _enabled = value;
            }
        }
        public bool Paused
        {
            get { return UpdateList.Instance.Paused; }
            set { UpdateList.Instance.Paused = value; }
        }
        private bool _enabled = true;
        public enum PauseType { Ballerina, Child, Regular }
        private PauseType CurrentPauseType = PauseType.Ballerina;
        private bool _pausedPushed = false;
        private float _flashPauseTimeRemaining = 0;

        Texture2D pauseImage; //= GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"BackGrounds\Screen_pause");
        Texture2D ballerinaImage; //= GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"BackGrounds\Attack_screen_ballerina");
        Texture2D childImage; //= GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"BackGrounds\Attack_screen_child");
        public void InitiateThreeSecondPause(PauseType newPause)
        {
            _flashPauseTimeRemaining = 3;
            Paused = true;
            CurrentPauseType = newPause;
        }
        public void Draw(GameTime gt, SpriteBatch sp)
        {
            if (!Paused)
                return;
            switch (CurrentPauseType)
            {
                case PauseType.Ballerina:
                    sp.Draw(ballerinaImage, new Rectangle(0,0, ballerinaImage.Width, ballerinaImage.Height), Color.White);
                    break;
                case PauseType.Child:
                    sp.Draw(childImage, new Rectangle(0, 0, childImage.Width, childImage.Height), Color.White);
                    break;
                case PauseType.Regular:
                    sp.Draw(pauseImage, new Rectangle(0, 0, pauseImage.Width, pauseImage.Height), Color.White);
                    break;
            }
        }
        public void Update(GameTime gt)
        {
            _flashPauseTimeRemaining -= gt.ElapsedGameTime.Milliseconds / 1000f;
            if (Paused)
            {
                if (CurrentPauseType == PauseType.Child || CurrentPauseType == PauseType.Ballerina)
                {
                    if (_flashPauseTimeRemaining < 0)
                    {
                        Paused = false;
                    }
                }

            }
            if (InputHandler.KeyDown(Keys.P) && _pausedPushed != true)
            {
                _pausedPushed = true;
            }
            if (_pausedPushed && InputHandler.KeyReleased(Keys.P))
            {
                if (Paused && CurrentPauseType == PauseType.Regular)
                    Paused = false;
                else if (!Paused)
                {
                    CurrentPauseType = PauseType.Regular;
                    Paused = true;
                }
            }
            
        }
        public static PauseManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new PauseManager();
                    _instance.init();
                }
                return _instance;
            }
        }

        private void init()
        {
            pauseImage = GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"BackGrounds\Screen_pause");
            ballerinaImage = GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"BackGrounds\Attack_screen_ballerina");
            childImage = GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"BackGrounds\Attack_screen_child");
        }

        private static PauseManager _instance;
    }
}
